function [L, p] = Cholesky(A)
n = size(A, 1);
L = zeros(n, n);
p = 0;
for k = 1 : n
    if k == 1
        L(k, k) = sqrt(A(k, k));
    else
        L(k, k) = sqrt(A(k, k) - sum(L(k, 1 : k - 1) .^ 2));
    end
    if  ~isreal(L(k, k)) || L(k, k) <= 0
        disp('A不是正定的');
        p = 1; % 表示A不是正定的
        return;
    end
    if k == 1   % 求下三角矩阵
        for i = k + 1 : n
            L(i, k) = A(i, k) / L(k, k);
        end
    else
        for i = k + 1 : n
            L(i, k) = (A(i, k) - sum(L(i, 1 : k - 1) .* L(k, 1 : k - 1))) / L(k, k);
        end
    end
end
end
